//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LMMNormalDriftCalculator.h"
using namespace Cephei::QL::Models::Marketmodels::Driftcomputation;
#include <gen/QL/Models/Marketmodels/Curvestates/LMMCurveState.h>
#include <gen/QL/Math/Matrix.h>
using namespace Cephei::QL::Models::Marketmodels::Curvestates;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::CLMMNormalDriftCalculator (Cephei::QL::Math::IMatrix^ pseudo, Cephei::IVector<Double>^ taus, UInt64 numeraire, UInt64 alive) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CMatrix^ _Cpseudo;
    CoVector<Double>^ _Ctaus;
    try
    {
#ifdef HANDLE
        _phLMMNormalDriftCalculator = NULL;
#endif
        _Cpseudo = safe_cast<CMatrix^> (pseudo);
        _Cpseudo->Lock();
        QuantLib::Matrix& _pseudo = static_cast<QuantLib::Matrix&> (_Cpseudo->GetReference ()); 
        CoVector<Double>^ _Ctaus = safe_cast<CoVector<Double>^> (taus);
        _Ctaus->Lock();
        INativeVector<Double>^ _NCItaus = _Ctaus->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCtaus = safe_cast<CDoubleVector^>(_NCItaus);
        std::vector<QuantLib::Time>& _taus = static_cast<std::vector<QuantLib::Time>&> (_NCtaus->GetReference ());
        QuantLib::Size _numeraire = (QuantLib::Size)ValueHelper::Convert (numeraire);
        QuantLib::Size _alive = (QuantLib::Size)ValueHelper::Convert (alive);
        _ppLMMNormalDriftCalculator = new boost::shared_ptr<QuantLib::LMMNormalDriftCalculator> (new QuantLib::LMMNormalDriftCalculator ( _pseudo,  _taus,  _numeraire,  _alive ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpseudo != nullptr) _Cpseudo->Unlock();
        if (_Ctaus != nullptr) _Ctaus->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::CLMMNormalDriftCalculator (boost::shared_ptr<QuantLib::LMMNormalDriftCalculator>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLMMNormalDriftCalculator = NULL;
#endif
	_ppLMMNormalDriftCalculator = &childNative;
    
}
Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::CLMMNormalDriftCalculator (QuantLib::LMMNormalDriftCalculator& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phLMMNormalDriftCalculator = NULL;
#endif
	_ppLMMNormalDriftCalculator = new boost::shared_ptr<QuantLib::LMMNormalDriftCalculator> (&childNative);
    
    _LMMNormalDriftCalculatorOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::CLMMNormalDriftCalculator (CLMMNormalDriftCalculator^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLMMNormalDriftCalculator = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLMMNormalDriftCalculator = new boost::shared_ptr<QuantLib::LMMNormalDriftCalculator> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::CLMMNormalDriftCalculator (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLMMNormalDriftCalculator = NULL;
#endif
	if (!t->IsSubclassOf(CLMMNormalDriftCalculator::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::CLMMNormalDriftCalculator (QuantLib::Handle<QuantLib::LMMNormalDriftCalculator>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLMMNormalDriftCalculator = &childNative;
	_ppLMMNormalDriftCalculator = &static_cast<boost::shared_ptr<QuantLib::LMMNormalDriftCalculator>>(childNative.currentLink());
    
    _LMMNormalDriftCalculatorOwner = owner;
}
Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::CLMMNormalDriftCalculator (QuantLib::Handle<QuantLib::LMMNormalDriftCalculator> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLMMNormalDriftCalculator = &childNative;
	_ppLMMNormalDriftCalculator = &static_cast<boost::shared_ptr<QuantLib::LMMNormalDriftCalculator>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::CLMMNormalDriftCalculator (QuantLib::LMMNormalDriftCalculator childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLMMNormalDriftCalculator = NULL;
#endif
	_ppLMMNormalDriftCalculator = new boost::shared_ptr<QuantLib::LMMNormalDriftCalculator> (new QuantLib::LMMNormalDriftCalculator (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::~CLMMNormalDriftCalculator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppLMMNormalDriftCalculator != NULL)
    {
	    delete _ppLMMNormalDriftCalculator;
        _ppLMMNormalDriftCalculator = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::!CLMMNormalDriftCalculator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppLMMNormalDriftCalculator != NULL)
    {
	    delete _ppLMMNormalDriftCalculator;
    }
}
QuantLib::LMMNormalDriftCalculator& Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::GetReference ()
{
    if (_ppLMMNormalDriftCalculator == NULL) throw gcnew NativeNullException ();
	return **_ppLMMNormalDriftCalculator;
}
boost::shared_ptr<QuantLib::LMMNormalDriftCalculator>& Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::GetShared ()
{
    if (_ppLMMNormalDriftCalculator == NULL) throw gcnew NativeNullException ();
	return *_ppLMMNormalDriftCalculator;
}
QuantLib::LMMNormalDriftCalculator* Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::GetPointer ()
{
    if (_ppLMMNormalDriftCalculator == NULL) throw gcnew NativeNullException ();
	return &**_ppLMMNormalDriftCalculator;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LMMNormalDriftCalculator>& Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::GetHandle ()
{
	if (_phLMMNormalDriftCalculator == NULL)
	{
		_phLMMNormalDriftCalculator = new Handle<QuantLib::LMMNormalDriftCalculator> (*_ppLMMNormalDriftCalculator);
	}
	return *_phLMMNormalDriftCalculator;
}
#endif
bool Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::HasNative () 
{
	return (_ppLMMNormalDriftCalculator != NULL);
}

Cephei::QL::Models::Marketmodels::Driftcomputation::ILMMNormalDriftCalculator^ Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::Compute (Cephei::IVector<Double>^ fwds, Cephei::IVector<Double>^ drifts)
{
    CoVector<Double>^ _Cfwds;
    CoVector<Double>^ _Cdrifts;
    try
    {
        _Cfwds = safe_cast<CoVector<Double>^> (fwds);
        _Cfwds->Lock();
        INativeVector<Double>^ _NCIfwds = _Cfwds->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfwds = safe_cast<CDoubleVector^>(_NCIfwds);
        std::vector<QuantLib::Rate>& _fwds = static_cast<std::vector<QuantLib::Rate>&> (_NCfwds->GetReference ());
        _Cdrifts = safe_cast<CoVector<Double>^> (drifts);
        _Cdrifts->Lock();
        INativeVector<Double>^ _NCIdrifts = _Cdrifts->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdrifts = safe_cast<CDoubleVector^>(_NCIdrifts);
        std::vector<QuantLib::Real>& _drifts = static_cast<std::vector<QuantLib::Real>&> (_NCdrifts->GetReference ());
    	(*_ppLMMNormalDriftCalculator)->compute ( _fwds,  _drifts );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cfwds != nullptr) _Cfwds->Unlock();
        if (_Cdrifts != nullptr) _Cdrifts->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Driftcomputation::ILMMNormalDriftCalculator^ Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::Compute (Cephei::QL::Models::Marketmodels::Curvestates::ILMMCurveState^ cs, Cephei::IVector<Double>^ drifts)
{
    CLMMCurveState^ _Ccs;
    CoVector<Double>^ _Cdrifts;
    try
    {
        _Ccs = safe_cast<CLMMCurveState^> (cs);
        _Ccs->Lock();
        QuantLib::LMMCurveState& _cs = static_cast<QuantLib::LMMCurveState&> (_Ccs->GetReference ()); 
        _Cdrifts = safe_cast<CoVector<Double>^> (drifts);
        _Cdrifts->Lock();
        INativeVector<Double>^ _NCIdrifts = _Cdrifts->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdrifts = safe_cast<CDoubleVector^>(_NCIdrifts);
        std::vector<QuantLib::Real>& _drifts = static_cast<std::vector<QuantLib::Real>&> (_NCdrifts->GetReference ());
    	(*_ppLMMNormalDriftCalculator)->compute ( _cs,  _drifts );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccs != nullptr) _Ccs->Unlock();
        if (_Cdrifts != nullptr) _Cdrifts->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Driftcomputation::ILMMNormalDriftCalculator^ Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::ComputePlain (Cephei::IVector<Double>^ fwds, Cephei::IVector<Double>^ drifts)
{
    CoVector<Double>^ _Cfwds;
    CoVector<Double>^ _Cdrifts;
    try
    {
        _Cfwds = safe_cast<CoVector<Double>^> (fwds);
        _Cfwds->Lock();
        INativeVector<Double>^ _NCIfwds = _Cfwds->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfwds = safe_cast<CDoubleVector^>(_NCIfwds);
        std::vector<QuantLib::Rate>& _fwds = static_cast<std::vector<QuantLib::Rate>&> (_NCfwds->GetReference ());
        _Cdrifts = safe_cast<CoVector<Double>^> (drifts);
        _Cdrifts->Lock();
        INativeVector<Double>^ _NCIdrifts = _Cdrifts->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdrifts = safe_cast<CDoubleVector^>(_NCIdrifts);
        std::vector<QuantLib::Real>& _drifts = static_cast<std::vector<QuantLib::Real>&> (_NCdrifts->GetReference ());
    	(*_ppLMMNormalDriftCalculator)->computePlain ( _fwds,  _drifts );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cfwds != nullptr) _Cfwds->Unlock();
        if (_Cdrifts != nullptr) _Cdrifts->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Driftcomputation::ILMMNormalDriftCalculator^ Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::ComputePlain (Cephei::QL::Models::Marketmodels::Curvestates::ILMMCurveState^ cs, Cephei::IVector<Double>^ drifts)
{
    CLMMCurveState^ _Ccs;
    CoVector<Double>^ _Cdrifts;
    try
    {
        _Ccs = safe_cast<CLMMCurveState^> (cs);
        _Ccs->Lock();
        QuantLib::LMMCurveState& _cs = static_cast<QuantLib::LMMCurveState&> (_Ccs->GetReference ()); 
        _Cdrifts = safe_cast<CoVector<Double>^> (drifts);
        _Cdrifts->Lock();
        INativeVector<Double>^ _NCIdrifts = _Cdrifts->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdrifts = safe_cast<CDoubleVector^>(_NCIdrifts);
        std::vector<QuantLib::Real>& _drifts = static_cast<std::vector<QuantLib::Real>&> (_NCdrifts->GetReference ());
    	(*_ppLMMNormalDriftCalculator)->computePlain ( _cs,  _drifts );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccs != nullptr) _Ccs->Unlock();
        if (_Cdrifts != nullptr) _Cdrifts->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Driftcomputation::ILMMNormalDriftCalculator^ Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::ComputeReduced (Cephei::IVector<Double>^ fwds, Cephei::IVector<Double>^ drifts)
{
    CoVector<Double>^ _Cfwds;
    CoVector<Double>^ _Cdrifts;
    try
    {
        _Cfwds = safe_cast<CoVector<Double>^> (fwds);
        _Cfwds->Lock();
        INativeVector<Double>^ _NCIfwds = _Cfwds->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfwds = safe_cast<CDoubleVector^>(_NCIfwds);
        std::vector<QuantLib::Rate>& _fwds = static_cast<std::vector<QuantLib::Rate>&> (_NCfwds->GetReference ());
        _Cdrifts = safe_cast<CoVector<Double>^> (drifts);
        _Cdrifts->Lock();
        INativeVector<Double>^ _NCIdrifts = _Cdrifts->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdrifts = safe_cast<CDoubleVector^>(_NCIdrifts);
        std::vector<QuantLib::Real>& _drifts = static_cast<std::vector<QuantLib::Real>&> (_NCdrifts->GetReference ());
    	(*_ppLMMNormalDriftCalculator)->computeReduced ( _fwds,  _drifts );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cfwds != nullptr) _Cfwds->Unlock();
        if (_Cdrifts != nullptr) _Cdrifts->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Driftcomputation::ILMMNormalDriftCalculator^ Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator::ComputeReduced (Cephei::QL::Models::Marketmodels::Curvestates::ILMMCurveState^ cs, Cephei::IVector<Double>^ drifts)
{
    CLMMCurveState^ _Ccs;
    CoVector<Double>^ _Cdrifts;
    try
    {
        _Ccs = safe_cast<CLMMCurveState^> (cs);
        _Ccs->Lock();
        QuantLib::LMMCurveState& _cs = static_cast<QuantLib::LMMCurveState&> (_Ccs->GetReference ()); 
        _Cdrifts = safe_cast<CoVector<Double>^> (drifts);
        _Cdrifts->Lock();
        INativeVector<Double>^ _NCIdrifts = _Cdrifts->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdrifts = safe_cast<CDoubleVector^>(_NCIdrifts);
        std::vector<QuantLib::Real>& _drifts = static_cast<std::vector<QuantLib::Real>&> (_NCdrifts->GetReference ());
    	(*_ppLMMNormalDriftCalculator)->computeReduced ( _cs,  _drifts );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccs != nullptr) _Ccs->Unlock();
        if (_Cdrifts != nullptr) _Cdrifts->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Driftcomputation::ILMMNormalDriftCalculator^ Cephei::QL::Models::Marketmodels::Driftcomputation::CLMMNormalDriftCalculator_Factory::Create (Cephei::QL::Math::IMatrix^ pseudo, Cephei::IVector<Double>^ taus, UInt64 numeraire, UInt64 alive)
{
    return gcnew CLMMNormalDriftCalculator ( pseudo,  taus,  numeraire,  alive);
}
